var $table = $("#webUserTab");

$(function () {
    initTable();
    //查询事件
    $("#queryWebUserBtn").click(function () {
        var name = $('#name').val();
        var phone = $('#phone').val();
        //使用post
        $table.bootstrapTable('refresh', {
            silent: false,
            url: '../webuser/getWebUserByPage.htm',
            query:{"name": name, "phone": phone}
        });
    });
});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        url: '../webuser/getWebUserByPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "用户名",
                field: "name",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "昵称",
                field: "nickname",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "性别",
            	field: "sexId",
            	align: "center",
            	formatter: function(value, row, index){
            		var sex = "";
            		if(row.sexId == 0){
            			sex = "男";
            		}else if(row.sexId == 1){
            			sex = "女";
            		}
            		return sex;
            	}
            },
            {
                title: "头像",
                field: "headImgUrl",
                align: 'center',
                formatter: function(value, row, index){
                	var imgSrc = 'http://ossimg.zhikuchina.com.cn/' + row.headImg + "?x-oss-process=image/resize,m_lfit,h_48,w_48";
                	if(null == row.headImgUrl || row.headImgUrl.length == 0){
                		if(row.sexId == 1){
                			imgSrc = '../img/woman_heanimg.png';
                		}else {
                			imgSrc = '../img/man_headimg.png';
                		}
                	}
                	var e = '<img class="carousel-inner img-responsive center-block" style="width:40px;height:40px" src="' + imgSrc + '"/>';
                	return e;
                }
            },
            {
            	title: "手机号",
            	field: "phone",
            	align: "center",
            	valign: "middle"
            },
            {
                title: "省-市",
                field: "province-city",
                align: 'center',
                formatter: function(value, row, index){
                	var province = row.province;
                	var city = row.city;
                	if(province ==null){
                		province = '-';
                	}
                	if(city ==null){
                		city = '-';
                	}
                	return province + city;
                }
            },
            {
                title:"联系方式",
                field:"contact",
                align: 'center',
                valign: 'middle'
            },
            {
            	title:"备注",
            	field:"remark",
            	align: 'center',
            	valign: 'middle'
            },
            {
            	title:"添加时间",
            	field:"insTime",
            	align: 'center',
            	valign: 'middle'
            }/*,
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                    var d = '';
                    if (row.salesmanIsDelete == 0){
                        d = '<button class="btn btn-xs btn-danger" type="button" onclick="deleteSysUser(\'' + row.id + '\',\'' + row.name + '\',\'' + 1 + '\')"><i class="fa fa-trash-o"></i> 禁用 </button> ';
                    }else if(row.salesmanIsDelete == 1){
                        d = '<button class="btn btn-xs btn-warning" type="button" onclick="deleteSysUser(\'' + row.id + '\',\'' + row.name + '\',\'' + 0 + '\')"><i class="fa fa-trash-o"></i> 启用 </button> ';
                    }
                    return d;
                }
            }*/
        ]
    });
}

function deleteSysUser(userId, name,state) {
    layer.confirm('你确定要修改【' + name + '】用户状态吗？', {
        btn: ['修改', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "",
            cache: false,
            type: "get",
            dataType: "json",
            data: {'id': userId,'state':state},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: ''});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}